package com.fau.om.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.om.action.PublicAction;
import com.fau.om.entity.MedicalAdmin;
import com.fau.om.entity.MedicalOrdersPass;
import com.fau.om.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 订单取消Action
 * @author kangyj
 * @datetime 2014年11月27日,上午10:53:11
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class CancelAction extends PublicAction implements
		ModelDriven<MedicalOrdersPass> {

	private static final long serialVersionUID = 112523327431351615L;
	
	private MedicalOrdersPass ordersPass = new MedicalOrdersPass();
	
	/**
	 * @desc 订单取消页面初始化
	 * @return SUCCESS
	 * @author kangyj
	 * @datetime 2014年11月27日,上午10:50:48
	 */
	@Action(value = "cancel_init", results = {@Result(name = SUCCESS, location = "cancel_init.jsp")})
	public String init() {
		return SUCCESS;
	}
	
	/**
	 * @desc 分页查询订单取消
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月27日,下午1:23:06
	 */
	@Action(value = "cancel_view")
	public String view() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		ordersPass.setHospitalId(admin.getEnterpriseId());
		pageBean = this.baseAllService.getMaintainManagerService().findPassByPage(ordersPass, map);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}

	/**
	 * @desc 订单取消
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月27日,下午2:53:18
	 */
	@Action(value = "cancel_cancel")
	public String cancel() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(ordersPass.getId())) {
			boolean flag = this.baseAllService.getMaintainManagerService().cancelOrders(ordersPass);
			if(flag) {
				map.put("success", "true");
			} else {
				map.put("success", "fail");
			}
		}else {
			map.put("success", "false");
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 订单维护-订单取消导出当前页
	 * @return null
	 * @author dangjh
	 * @datetime 2014年10月20日,上午11:39:19
	 */
	@Action(value = "cancel_init_export_current")
	public String currentExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids = request.getParameter("ids");
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"订单取消表.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getMaintainManagerService().exportOrdersCurrent(ids);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @desc 订单维护-订单取消导出所有
	 * @return null
	 * @author dangjh
	 * @datetime 2014年10月21日,下午11:51:37
	 */
	@Action(value = "cancel_init_export_all")
	public String allExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"订单取消表.xls"));
			response.setContentType("application/vnd.ms-excel");
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			ordersPass.setHospitalId(admin.getEnterpriseId());
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getMaintainManagerService().exportOrdersAll(ordersPass);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public MedicalOrdersPass getModel() {
		return ordersPass;
	}

}
